package com.lowca.news.web.controller.admin;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.codehaus.jackson.map.ObjectMapper;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.lowca.news.entity.Category;
import com.lowca.news.service.CategoryService;
import com.lowca.news.web.formbean.AddCategoryForm;
import com.lowca.news.web.jsonbean.IdNameBean;

@Controller
public class AdminCategoryController {

	@Resource
	private CategoryService categoryService;

	@RequestMapping(value = "/admin/category/get-by-level.do", method = RequestMethod.GET)
	@ResponseBody
	public List<IdNameBean> getDate(HttpServletRequest request,
			HttpServletResponse response,
			@RequestParam(value = "level", required = true) int level)
			throws ServletException, IOException {
		List<Category> list = categoryService.findCategoryByLevel(level);
		List<IdNameBean> idNameList = new ArrayList<IdNameBean>();
		for (Category c : list) {
			idNameList.add(new IdNameBean(c.getId(), c.getName()));
		}
		return idNameList;
	}

	@RequestMapping(value = "/admin/category/add.do", method = RequestMethod.GET)
	public String userList(HttpServletRequest request) throws ServletException,
			IOException {
		return "/admin/category/add";
	}

	@RequestMapping(value = "/admin/category/add.do", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> addCategorySubmit(HttpServletRequest request,
			HttpServletResponse response,
			@RequestParam(value = "jForm", required = true) String jForm)
			throws ServletException, IOException {
		// 接收对象
		ObjectMapper mapper = new ObjectMapper();
		mapper.registerSubtypes(AddCategoryForm.class);
		AddCategoryForm form = mapper.readValue(jForm, AddCategoryForm.class);
		// 验证
		return null;
	}
}
